package com.espeaker.sdk.api;

import android.os.Handler;

import com.espeaker.sdk.Constants;
import com.espeaker.sdk.ESpeakerParameters;
import com.espeaker.sdk.model.ReviseJson;
import com.espeaker.sdk.utils.Base64Util;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class ReviseApi extends MiaoApi {
	/*
	 * 获取工资和批改条数
	 */
	public void getSalary(final String userId, final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("reviser_id", userId, true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_GET_SALARY, params);
					if (handler != null) {
						handler
								.obtainMessage(Constants.REVISE_GET_SALARY,
										mRes).sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}

	/*
	 * 拉取订单
	 */
	public void pull(final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("reviser_id", "userId", true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_PULL, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_PULL, mRes)
								.sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}

	/*
	 * 获取未完成订单
	 */
	public void getUnfinished(final String reviser_id, final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("reviser_id", reviser_id, true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_UNFINISHED, params);
					if (handler != null) {
						handler
								.obtainMessage(Constants.REVISE_UNFINISHED,
										mRes).sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}

	/*
	 * 拒绝订单
	 */
	public void reject(final String xiaofei_id, final String rejected_reason,
			final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("xiaofei_id", xiaofei_id, true);
					params.add("rejected_reason", Base64Util.encodeStr(rejected_reason), true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_REJECT, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_REJECT, mRes)
								.sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}

	/*
	 * 抢单
	 */
	public void grab(final String reviser_id, final String xiaofei_id,
			final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("reviser_id", reviser_id, true);
					params.add("xiaofei_id", xiaofei_id, true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_GRAB, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_GRAB, mRes)
								.sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}

	/*
	 * 提交批改
	 */
	public void submit(final ReviseJson json, final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					String revise_user_points = "";
					String revise_reviser_points = "";
					try {
						for (int i = 0; i < json.revise_user_points.length; i++) {
							if(json.revise_user_points[i].duration>0){
							revise_user_points = revise_user_points
									+ json.revise_user_points[i] + ",";
							}
						}
						if (revise_user_points.length() > 0) {
							revise_user_points = revise_user_points.substring(
									0, revise_user_points.length() - 1);
						}
						revise_user_points = "[" + revise_user_points + "]";
					} catch (Exception er) {

					}
					try {
						for (int i = 0; i < json.revise_reviser_points.length; i++) {
							if(json.revise_reviser_points[i].duration>0){
							revise_reviser_points = revise_reviser_points
									+ json.revise_reviser_points[i] + ",";
							}
						}
						if (revise_reviser_points.length() > 0) {
							revise_reviser_points = revise_reviser_points
									.substring(0, revise_reviser_points
											.length() - 1);
						}
						revise_reviser_points = "[" + revise_reviser_points
								+ "]";
					} catch (Exception er) {
					}
					ESpeakerParameters params = new ESpeakerParameters();
					//params.add("audio_url", json.audio_url, true);
					params.add("audio_length", json.audio_length, true);
					params.add("title", json.title, true);
					params.add("delivery_score", json.delivery_score, true);
					params.add("language_score", json.language_score, true);
					params.add("topic_score", json.topic_score, true);
					params.add("revise_audio", json.revise_audio, false);
					params.add("suggestion_revise_audio", json.suggestion_revise, false);
					params.add("overall_revise_audio", json.overall_revise, false);
					params.add("xiaofei_id", json.xiaofei_id, true);
					params.add("reviser_id", json.revise_id, true);
					params.add("revise_user_points", revise_user_points, true);
					params.add("revise_reviser_points", revise_reviser_points,
							true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_SUBMIT, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_SUBMIT, mRes)
								.sendToTarget();
					}
					return;
				} catch (Exception er) {
				}
				if (handler != null) {
					handler.obtainMessage(Constants.REVISE_SUBMIT, "")
							.sendToTarget();
				}
			}
		}.start();
	}

	/*
	 * 获取题目
	 */
	public void getTimu(final String tiku_title,
			final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("tiku_title", tiku_title, true);
					//params.add("tiku_type", tike_type, true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_GETQuesiont, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_GETQUESTION,
								mRes).sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}
	/*
	 * 获取反馈
	 */
	public void feedback(final String reviser_id,
			final Handler handler) {
		new Thread() {
			@Override
			public void run() {
				try {
					ESpeakerParameters params = new ESpeakerParameters();
					params.add("reviser_id", reviser_id, true);
					String mRes = request(MiaoApi.GetBaseURL(), REVISE_FEEDBACK, params);
					if (handler != null) {
						handler.obtainMessage(Constants.REVISE_FEEDBACK,
								mRes).sendToTarget();
					}
				} catch (Exception er) {
				}
			}
		}.start();
	}
}
